﻿using System;
using System.Web.UI.WebControls;
using Controllers;
using EntityLayer;
using EntityLayer.Entities;
using Utils;

public partial class SalesItems : BasePage, ISalesItemManagementView, ICourseManagementView
{
    private CourseController courseController;
    private string err;
    private SalesItemController salesItemController;
    protected string ErrorMessage
    {
        get { return err; }
        set { err = value.Replace('\'', ' '); }
    }
    /// <summary>
    /// this page is restricted to registered users
    /// </summary>
    protected override Enumerations.UserPrivileges UserPrivileges
    {
        get { return Enumerations.UserPrivileges.StandardUser; }
    }

    #region ICourseManagementView Members
    public event EventHandler<CourseEventArgs> CreateCourseEvent;
    public event EventHandler<CourseEventArgs> UpdateCourseEvent;
    #endregion

    #region ISalesItemManagementView Members
    /// <summary>
    /// fill all of the controls with dynamic data
    /// </summary>
    public override void BindData()
    {
        dlCourses.DataSource = Course.FindAll();
        dlCourses.DataBind();
        dlBooks.DataSource = Book.FindAll();
        dlBooks.DataBind();
        dlMedia.DataSource = Media.FindAll();
        dlMedia.DataBind();
        ddMediaTypes.DataSource = Enumerations.GetAllEnumerationsForDisplay<Enumerations.MediaTypes>();
        ddMediaTypes.DataBind();
        lstBookCourses.DataSource = Course.FindAll();
        lstBookCourses.DataBind();
        lstMediaCourses.DataSource = Course.FindAll();
        lstMediaCourses.DataBind();
    }
    public event EventHandler<SalesItemEventArgs> CreateSalesItemEvent;
    public event EventHandler<SalesItemEventArgs> UpdateSalesItemEvent;
    #endregion

    protected override void WireEvents()
    {
    }
    /// <summary>
    /// this page serves as two views at the same time, so we must register two controllers
    /// to respond to the user-generated events
    /// </summary>
    protected override void InitializeSupervisingController()
    {
        courseController = new CourseController(this);
        salesItemController = new SalesItemController(this);
    }
    /// <summary>
    /// add a new course
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddCourse_Click(object sender, EventArgs e)
    {
        try
        {
            CreateCourseEvent(this, new CourseEventArgs(new Course(DateTime.Now, txtCourseNameAndNumber.Text)));
        }
        //exceptions bubble back up to the user
        catch (AssertionException exp)
        {
            ErrorMessage = exp.Message;
        }
        BindData();
    }
    /// <summary>
    /// add a new book
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnInsertBook_Click(object sender, EventArgs e)
    {
        try
        {
            Book book = new Book();
            book.Author = txtBookAuthor.Text;
            book.Isbn = txtBookISBN.Text;
            book.Name = txtBookName.Text;
            book.Year = int.Parse(txtBookYear.Text);
            book.NumPages = int.Parse(txtBookNumPages.Text);
            book.PostDate = DateTime.Now;
            book.User = LoggedInUser;
            book.SalesItemStatusType = Enumerations.SalesItemStatusTypes.Open;
            book.Price = decimal.Parse(txtBookPrice.Text);
            foreach (ListItem _item in lstBookCourses.Items)
            {
                if(_item.Selected)
                {
                    book.Courses.Add(Course.Find(int.Parse(_item.Value)));
                }
            }
            CreateSalesItemEvent(this, new SalesItemEventArgs(book));
            BindData();
        }
        //exceptions bubble back up to the user
        catch (Exception exp)
        {
            ErrorMessage = exp.Message;
        }
    }
    /// <summary>
    /// add a new media
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddMedia_Click(object sender, EventArgs e)
    {
        try
        {
            Media media = new Media();
            media.Name = txtMediaName.Text;
            media.Year = int.Parse(txtMediaYear.Text);
            media.PostDate = DateTime.Now;
            media.User = LoggedInUser;
            media.SalesItemStatusType = Enumerations.SalesItemStatusTypes.Open;
            media.Price = decimal.Parse(txtMediaPrice.Text);
            media.MediaType = (Enumerations.MediaTypes)(Enum.Parse(typeof (Enumerations.MediaTypes), ddMediaTypes.SelectedValue));
            foreach (ListItem _item in lstMediaCourses.Items)
            {
                if (_item.Selected)
                {
                    media.Courses.Add(Course.Find(int.Parse(_item.Value)));
                }
            }
            CreateSalesItemEvent(this, new SalesItemEventArgs(media));
            BindData();
        }
        //exceptions bubble back up to the user
        catch (Exception exp)
        {
            ErrorMessage = exp.Message;
        }
    }
    /// <summary>
    /// inner data binding between salesitems and their courses
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dlBooks_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
        Repeater _repeater = e.Item.FindControl("rptCourses") as Repeater;
        SalesItem _salesItem = e.Item.DataItem as SalesItem;
        _repeater.DataSource = _salesItem.Courses;
        _repeater.DataBind();
    }
}